/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
//解法一
class Solution {
    public ListNode removeZeroSumSublists(ListNode head) {
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        ListNode prev = newHead;
        while(prev != null){
            int sum = 0;
            ListNode cur = prev.next;
            while(cur != null){
                sum += cur.val;
            if(sum == 0){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                cur = cur.next;
                }
            }
            prev = prev.next;
        }
        return newHead.next;
    }
}

//解法二,本质一样,就是多了个break和多判断了一下
class Solution {
    public ListNode removeZeroSumSublists(ListNode head) {
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        ListNode prev = newHead;
        while(prev != null){
            int sum = 0;
            ListNode cur = prev.next;
            while(cur != null){
                sum += cur.val;
            if(sum == 0){
                prev.next = cur.next;
                break;
            }else{
                cur = cur.next;
                }
            }
            if(cur == null) prev = prev.next;
        }
        return newHead.next;
    }
}